package com.xcc.singletonpattern;

/**
 * @Description: Singleton
 * @Author xiechuang
 * @Date 2022/9/6
 */
public class Singleton4 {

    // volatile关键字：变量线程间可见
    private volatile static Singleton4 uniqueInstance;

    private Singleton4() {}

    // version3："双重检查加锁"创建实例，jdk5以上使用
    public static synchronized Singleton4 getInstance2() {
        if (uniqueInstance == null) {
            synchronized (Singleton4.class) {
                if (uniqueInstance == null) {
                    uniqueInstance = new Singleton4();
                }
            }
        }
        return uniqueInstance;
    }

}
